package com.rentong.wx.cust.service.impl;

import com.rentong.basic.BusinessException;
import com.rentong.common.RandomUtils;
import com.rentong.wx.cust.dao.*;
import com.rentong.wx.cust.domain.*;
import com.rentong.wx.cust.dto.CustServiceOrderAdvEntityDto;
import com.rentong.wx.cust.service.CustServiceOrderAdvService;
import com.rentong.wx.sys.dao.SysConfChongzhiBstDao;
import com.rentong.wx.sys.dao.SysConfChongzhiChefCoinDao;
import com.rentong.wx.sys.dao.SysConfChongzhiHuiyuanDao;
import com.rentong.wx.sys.domain.SysConfChongzhiBstEntity;
import com.rentong.wx.sys.domain.SysConfChongzhiChefCoinEntity;
import com.rentong.wx.sys.domain.SysConfChongzhiHuiyuanEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class CustServiceOrderAdvServiceImpl implements CustServiceOrderAdvService {

    @Resource
    private CustServiceOrderAdvDao custServiceOrderAdvDao;

    @Resource
    private SysConfChongzhiChefCoinDao sysConfChongzhiChefCoinDao;

    @Resource
    private CustServiceOrderServiceImpl custServiceOrderServiceImpl;

    @Resource
    private SysConfChongzhiHuiyuanDao sysConfChongzhiHuiyuanDao;

    @Resource
    private SysConfChongzhiBstDao sysConfChongzhiBstDao;

    @Resource
    private WxCustUserMapper wxCustUserMapper;

    @Resource
    private CustGetCanyinquanPhoneDao custGetCanyinquanPhoneDao;

    @Resource
    private CustGetServiceOrderPhoneDao custGetServiceOrderPhoneDao;

    @Resource
    private CustCanyinquanDao custCanyinquanDao;

    private String[] danjuNoHead = {"YHCZ", "CSCZ", "YHCYQDH", "CSCYQDH", "YHBSTCYQ", "CSBSTCYQ",
            "YHBSTCS", "CSBSTCS", "YHBSTDD", "CSBSTDD", "YHHY", "CSHY", "YHDDDH", "CSDDDH"};


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object insertAdvOrder(CustServiceOrderAdvEntity csoa) throws Exception{
        String order = RandomUtils.getOrderNo();
        Long orderNo = Long.parseLong(order.substring(0, order.length() - 1));
        //生成预支付订单
        Long payId = Long.parseLong(RandomUtils.getPaymentNo());

        csoa.setServiceOrderId(orderNo);
        csoa.setDanjuNo(danjuNoHead[csoa.getOrderType() - 1] + orderNo);
        csoa.setOrderState(1);
        csoa.setPayId(payId);
        csoa.setAddTime(new Date());


        //订单类型
        if (csoa.getOrderType() == 1 || csoa.getOrderType() == 2) {
            // 1：用户充值；2：厨师充值；
            csoa.setRelationName(csoa.getOrderType() == 1 ? "用户充值" : "厨师充值");

            SysConfChongzhiChefCoinEntity sccc = sysConfChongzhiChefCoinDao.selectByPrimaryKey(csoa.getRelationId());
            csoa.setGetAmount(sccc.getChefCoinAmount());
            csoa.setPayAmount(sccc.getMoneyAmount());

        }else if (csoa.getOrderType() == 3 || csoa.getOrderType() == 4) {
            // 3：用户购买餐饮圈电话；4：厨师购买餐饮圈电话；
            csoa.setRelationName(csoa.getOrderType() == 3 ? "用户购买餐饮圈电话" : "厨师购买餐饮圈电话");
            csoa.setRelationId(csoa.getRelationId());
            csoa.setPayAmount(new BigDecimal("100"));

        }else if (csoa.getOrderType() == 13 || csoa.getOrderType() == 14) {
            // 13：用户购买订单电话；14：厨师购买订单电话；
            csoa.setRelationName(csoa.getOrderType() == 13 ? "用户购买订单电话" : "厨师购买订单电话");
            csoa.setPayAmount(new BigDecimal("100"));

        }else if (csoa.getOrderType() == 5 || csoa.getOrderType() == 6 || csoa.getOrderType() == 7 ||
                csoa.getOrderType() == 8 || csoa.getOrderType() == 9 || csoa.getOrderType() == 10) {
            // 5：用户购买餐饮圈的帮上推；6：厨师购买餐饮圈的帮上推；
            // 7：用户购买厨师本身的帮上推；8：厨师购买厨师本身的帮上推；
            // 9：用户购买单子的帮上推；10：厨师购买单子的帮上推；
            if (csoa.getOrderType() == 5) {
                csoa.setRelationName("用户购买餐饮圈的帮上推");
            } else if (csoa.getOrderType() == 6) {
                csoa.setRelationName("厨师购买餐饮圈的帮上推");
            } else if (csoa.getOrderType() == 7) {
                csoa.setRelationName("用户购买厨师本身的帮上推");
            } else if (csoa.getOrderType() == 8) {
                csoa.setRelationName("厨师购买厨师本身的帮上推");
            } else if (csoa.getOrderType() == 9) {
                csoa.setRelationName("用户购买单子的帮上推");
            } else if (csoa.getOrderType() == 10) {
                csoa.setRelationName("厨师购买单子的帮上推");
            }

            SysConfChongzhiBstEntity sccb = sysConfChongzhiBstDao.selectByPrimaryKey(csoa.getRelationId());

            //如果是免费的，那么扣掉一次机会
            if (sccb.getBstAmount().toString().equals("0")) {
                WxCustUser wxCustUser = wxCustUserMapper.selectByPrimaryKey(csoa.getCustId());
                WxCustUser wc = new WxCustUser();
                wc.setCustId(wxCustUser.getCustId());
                wc.setHuiyuanNum(0);
                wc.setUpdateTime(new Date());
                wxCustUserMapper.updateByPrimaryKeySelective(wc);
            }

            csoa.setGetAmount(new BigDecimal(sccb.getBstDayNum()));
            csoa.setPayAmount(sccb.getBstAmount());

        }else if (csoa.getOrderType() == 11 || csoa.getOrderType() == 12) {
            // 11：用户购买会员；12：厨师购买会员；
            csoa.setRelationName(csoa.getOrderType() == 11 ? "用户购买会员" : "厨师购买会员");

            SysConfChongzhiHuiyuanEntity scch = sysConfChongzhiHuiyuanDao.selectByPrimaryKey(csoa.getRelationId());
            csoa.setPayAmount(scch.getHuiyuanAmount());
            csoa.setGetAmount(new BigDecimal(scch.getHuiyuanTimeNum().toString()));

        }else {
            throw new BusinessException("支付异常！");
        }

        custServiceOrderAdvDao.insertSelective(csoa);

        return custServiceOrderServiceImpl.toPay(csoa.getPayType(), csoa.getCustId(), orderNo, payId);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String getCanCallPhone(CustServiceOrderAdvEntityDto csoa) throws Exception{
        //先判断下是不是会员，会员的话，直接可以拨打
        Boolean isHuiyuan = false;
        WxCustUser wxCustUser = wxCustUserMapper.selectByPrimaryKey(csoa.getCustId());
        if (wxCustUser.getHuiyuanTime() != null && wxCustUser.getHuiyuanTime().after(new Date())) {
            isHuiyuan = true;
        }

        //查询下之前是否花过钱
        if (csoa.getCallType() == 1) {
            //1：餐饮圈中拨打电话
            CustGetCanyinquanPhoneEntity cgcp = custGetCanyinquanPhoneDao.getCanyinquanPhoneById(Long.parseLong(csoa.getId()), csoa.getCustId());
            int flag=0;
            if (cgcp != null) {
                //可以拨打电话
                WxCustUser wc = wxCustUserMapper.selectByPrimaryKey(cgcp.getCalledCustId());
                custCanyinquanDao.updatePhoneNum(Long.parseLong(csoa.getId()));
                return wc.getPhone();
            }else {
                //没有购买过
                if (isHuiyuan) {
                    //会员免购买
                    CustCanyinquanEntity cc = custCanyinquanDao.selectByPrimaryKey(Long.parseLong(csoa.getId()));
                    WxCustUser wc = wxCustUserMapper.selectByPrimaryKey(cc.getCustId());
                    custCanyinquanDao.updatePhoneNum(Long.parseLong(csoa.getId()));
                    return wc.getPhone();
                }else {
                    //不能拨打
                    return "1";
                }
            }

        }else {
            //2：其他情况拨打
            CustGetServiceOrderPhoneEntity cgso = custGetServiceOrderPhoneDao.getServiceOrderPhoneById(Long.parseLong(csoa.getId()), csoa.getCustId());

            if (cgso != null) {
                //可以拨打电话
                WxCustUser wc = wxCustUserMapper.selectByPrimaryKey(cgso.getCalledCustId());
                return wc.getPhone();
            }else {
                //没有购买过
                if (isHuiyuan) {
                    //会员免购买
                    WxCustUser wc = wxCustUserMapper.selectByPrimaryKey(Long.parseLong(csoa.getId()));
                    return wc.getPhone();
                }else {
                    //不能拨打
                    return "1";
                }
            }
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelAdvOrder(CustServiceOrderAdvEntity custServiceOrderAdvEntity){
        custServiceOrderAdvDao.deleteByPrimaryKey(custServiceOrderAdvEntity);
    }





}